#!/bin/bash

set -e

rm -rf bin
echo "cc_library(name='c', rcs=['c2.cc'], hdrs = ['c.h'])" > td/BUILD

cleanup_exit() {
  echo "Test failed" ${2}
  exit ${1}
}

echo "Build should fail"
../../build.py build :a && cleanup_exit 1 "build :a #1 succeded"
../../pretty_graph_dump.py bin/__graph__/newgraph.db | diff reference_graph1 - || cleanup_exit 1 "graph #1 diff"

echo "cc_library(name='c', srcs=['c2.cc'], hdrs = ['c.h'])" > td/BUILD

echo "Build should fail"
../../build.py build :a && cleanup_exit 1 "build :a #2 succeded"
../../pretty_graph_dump.py bin/__graph__/newgraph.db | diff reference_graph2 - || cleanup_exit 1 "graph #2 diff"

echo "cc_library(name='c', srcs=['c.cc'], hdrs = ['c.h'])" > td/BUILD

echo "Build should succeed"
../../build.py build :a || cleanup_exit 1 "build :a #3 failed"
../../pretty_graph_dump.py bin/__graph__/newgraph.db | diff reference_graph3 - || cleanup_exit 1 "graph #3 diff"

rm -rf bin

echo 'Test passed'
